#include "datamodel.h"

DataModel::DataModel(QObject *parent) :
    QAbstractTableModel(parent), cur(15)
{
    m_data.push_back(TableRow("January", 31, WINTER));
    m_data.push_back(TableRow("February", 28, WINTER));
    m_data.push_back(TableRow("Marth", 31, SPRING));
    m_data.push_back(TableRow("April", 30, SPRING));
    m_data.push_back(TableRow("May", 31, SPRING));
    m_data.push_back(TableRow("June", 30, SUMMER));
    m_data.push_back(TableRow("July", 31, SUMMER));
    m_data.push_back(TableRow("August", 31, SUMMER));
    m_data.push_back(TableRow("September", 30, FALL));
    m_data.push_back(TableRow("October", 31, FALL));
    m_data.push_back(TableRow("November", 30, FALL));
    m_data.push_back(TableRow("December", 31, WINTER));
}

int DataModel::rowCount(const QModelIndex &parent) const
{
    QVector<TableRow> rows = filter(cur);
    int size = rows.size();
    return size;
}

int DataModel::columnCount(const QModelIndex &parent) const
{
    return 2;
}

QVariant DataModel::data(const QModelIndex &index, int role) const
{
    if (role == Qt::DisplayRole && index.isValid())
    {
        QVector<TableRow> rows = filter(cur);
        if (index.column() == 0)
        {
            return rows[index.row()].month;
        }
        if (index.column() == 1)
        {
            return rows[index.row()].days;
        }
    }
    return QVariant();
}

void DataModel::setSeason(int s)
{
    cur = s;
    //updating viewer:
    emit this->layoutChanged();
}

QVector<TableRow> DataModel::filter(int seasons) const
{
    QVector<TableRow> v;
    for (int i = 0; i < m_data.size(); ++i)
    {
        TableRow tr= m_data[i];
        int r = tr.season & seasons;
        if (r != 0)
            v.push_back(tr);
    }
    return v;
}
